package com.jsmy.marketing.fg;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jsmy.marketing.R;
import com.jsmy.marketing.activity.CustomerActivity;
import com.jsmy.marketing.activity.PlanDetailActivity;
import com.jsmy.marketing.adapter.CustomerPlantFragmentAdapter;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.bean.BaseBean;
import com.jsmy.marketing.bean.YWgtjhBean;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by PC_WLT on 2018/8/27.
 */

public class CustomerPlantFragment extends BaseFragment {

    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    CustomerPlantFragmentAdapter adapter;
    List<YWgtjhBean.DataBean.ListBean> list;
    int pageindex = 1;
    @Override
    protected int resLayout() {
        return R.layout.fg_test_layout;
    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
        adapter = new CustomerPlantFragmentAdapter();
        myRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        myRecycler.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageindex = 1;
//                presenter.getzlgtplanlist(((CustomerActivity) mActivity).kid, pageindex);
                getzlgtplanlist(((CustomerActivity)getActivity()).kid);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageindex++;
                getzlgtplanlist(((CustomerActivity)getActivity()).kid);
//                presenter.getzlgtplanlist(((CustomerActivity) mActivity).kid, pageindex);
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.btn_confirm:
                        saverwflaginfo(list.get(position).getPlanid(),"Y");
                        break;
                }
            }

        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                YWgtjhBean.DataBean.ListBean listBean= (YWgtjhBean.DataBean.ListBean) adapter.getItem(position);
                Bundle b= new Bundle();
                b.putString("planid",listBean.getPlanid());
                openActivity(PlanDetailActivity.class,b);
            }
        });
        getzlgtplanlist(((CustomerActivity)getActivity()).kid);
    }

    private void saverwflaginfo(String plantId, String y) {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("planid", plantId);
        map.put("flat", y);
        mPresenter.loadData(RetrofitManager.getInstance().saverwflaginfo(map), Link.APP_SAVE_RW_FLAG_INFO);
    }

    private void getzlgtplanlist(String plantId) {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("kid", plantId);
        map.put("pageindex", "" + pageindex);
        mPresenter.loadData(RetrofitManager.getInstance().getzlgtplanlist(map), Link.APP_ZLGT_LPAN_LIST);
    }

    @Override
    public void refreshData(Object object, String position) {
        switch (position) {
            case Link.APP_ZLGT_LPAN_LIST:
                list = ((YWgtjhBean) object).getData().getList();
                if (pageindex == 1) {
                    refreshLayout.finishRefresh();
                    adapter.replaceData(list);
                } else {
                    refreshLayout.finishLoadmore();
                    adapter.addData(list);
                }
                break;
            case Link.APP_SAVE_RW_FLAG_INFO:
                BaseBean baseBean= (BaseBean) object;
                if (baseBean.getCode().equals("Y")){
                    ToastUtil.showShort(getContext(),"成功");
                    update();
                }else {
                    ToastUtil.showShort(getContext(),baseBean.getMsg());
                }
                break;
        }
    }
    @Override
    public void update() {
        pageindex = 1;
        getzlgtplanlist(((CustomerActivity)getActivity()).kid);
    }
}
